var th,
    url = 'Dadong/NEX',
    tabId = window.location.hash,
    // 缩写列表
    idLib = [
        {
            $id: $('#TX_DailySPHTarget'),
            val: 'D.SPH T.'
        },
        {
            $id: $('#TX_CurrentSPH'),
            val: 'C. SPH'
        },
        {
            $id: $('#TX_SPHDeviation'),
            val: 'SPH Dev.'
        },
        {
            $id: $('#TX_DailyUptimeTarget'),
            val: 'D. Uptime T.'
        },
        {
            $id: $('#TX_CurrentUptime'),
            val: 'C. Uptime'
        },
        {
            $id: $('#TX_UptimeDeviation'),
            val: 'Uptime Dev.'
        },
        {
            $id: $('#DD_DailySPHTarget'),
            val: 'D.SPH T.'
        },
        {
            $id: $('#DD_CurrentSPH'),
            val: 'C. SPH'
        },
        {
            $id: $('#DD_SPHDeviation'),
            val: 'SPH Dev.'
        },
        {
            $id: $('#DD_DailyUptimeTarget'),
            val: 'D. Uptime T.'
        },
        {
            $id: $('#DD_CurrentUptime'),
            val: 'C. Uptime'
        },
        {
            $id: $('#DD_UptimeDeviation'),
            val: 'Uptime Dev.'
        },
        {
            $id: $('#GP1930'),
            val: '1930'
        },
        {
            $id: $('#GP1960'),
            val: '1960'
        },
        {
            $id: $('#GP1961'),
            val: '1961'
        },
        {
            $id: $('#GP1965'),
            val: '1965'
        }

    ];

//定义页面大小与背景颜色
var setBackground = function () {
    var width = $(window).width();
    var height = $(window).height();
    var bodyHeight = parseInt($('#viewStage').css('height'));
    // $('.com-bg').css('width', width);
    if (bodyHeight > height) {
        $('.com-bg').css('height', bodyHeight);
    } else {
        $('.com-bg').css('height', height);
    }
    if (width < 450) {
        $.each(idLib, function (index, item) {
            item.$id.html(item.val);
        });
    }
};

var urlMark = [
    {
        tabID: '#Assembly',
        $tabID: $('#tab_Assembly'),
        mark: '#Assembly',
        $tableID: $('#table_Assembly')
    },
    {
        tabID: '#Paint',
        $tabID: $('#tab_Paint'),
        mark: '#Paint',
        $tableID: $('#table_paint')
    },
    {
        tabID: '#Body',
        $tabID: $('#tab_Body'),
        mark: '#Body',
        $tableID: $('#table_body')
    },
    {
        tabID: '#Press',
        $tabID: $('#tab_Press'),
        mark: '#Press',
        $tableID: $('#table_Press')
    }
];

//判断数字函数
var is_numberic = function (val) {
    var result = false;
    var pattern = new RegExp("^ *\\-{0,1} *\\d+(\\.\\d+){0,1} *$");
    var valType = typeof val;
    if (valType === 'string' || valType === 'number') {
        result = pattern.test(val)
    }
    return result;
};

//判断背景颜色函数
var judgeBackgroundColor = function (result, threshold, item) {
    var $elm = item.$elm;
    if (threshold !== 'none') {
        if (result < threshold) {
            $elm.css({
                'background': '#FF2700',
                'color': '#fff'
            });
            // $elm.prev().css({
            //     'background': '#FF2700',
            //     'color': '#fff'
            // });
        } else if (result >= threshold) {
            $elm.css({
                'background': '#008000',
                'color': '#fff'
            });
            // $elm.prev().css({
            //     'background': '#008000',
            //     'color': '#fff'
            // });
        }
    }
};

//处理Assembly数据函数
var getAssemblyValue = function (res, item) {
    var result = 0,
        dbId = item.dbId,
        threshold = item.threshold,
        flag = item.flag,
        value = res[dbId];
    switch (flag) {
        case 1:
            if (is_numberic(value)) {
                result = value;
                judgeBackgroundColor(result, threshold, item);
            } else {
                result = "--";
            }
            break;
        case 2:
            if (is_numberic(value)) {
                result = value + 's';
            } else {
                result = "--";
            }
            break;
    }
    return result;
};

//处理BodyShop数据函数
var getBodyValue = function (res, item) {
    var result = 0,
        dbId = item.dbId,
        dbId1 = item.dbId1,
        dbId2 = item.dbId2,
        threshold = item.threshold,
        flag = item.flag;
    switch (flag) {
        case 1:
            if (is_numberic(res[dbId])) {
                result = res[dbId];
                judgeBackgroundColor(result, threshold, item);
            } else {
                result = '--';
            }
            break;
        case 2:
            if (is_numberic(res[dbId]) && is_numberic(res[dbId1])) {
                result = res[dbId] + res[dbId1];
            } else {
                result = '--';
            }
            break;

        case 3:
            if (is_numberic(res[dbId]) && is_numberic(res[dbId1]) && is_numberic(res[dbId2])) {
                result = res[dbId] + res[dbId1] - res[dbId2];
                judgeBackgroundColor(result, threshold, item);
            } else {
                result = '--';
            }
            break;

        case 4:
            if (is_numberic(res[dbId]) && is_numberic(res[dbId1])) {
                result = res[dbId] - res[dbId1];
                judgeBackgroundColor(result, threshold, item);
            } else {
                result = '--';
            }
            break;
    }
    return result;

};

//处理PaintShop数据函数
var getPaintValue = function (res, item) {
    var result = 0,
        dbId = item.dbId,
        dbId1 = item.dbId1,
        dbId2 = item.dbId2,
        dbId3 = item.dbId3,
        threshold = item.threshold,
        flag = item.flag;
    switch (flag) {
        case 1:
            if (is_numberic(res[dbId])) {
                result = res[dbId];
                judgeBackgroundColor(result, threshold, item);
            } else {
                result = '--';
            }
            break;
        case 2:
            if (is_numberic(res[dbId]) && is_numberic(res[dbId1])) {
                result = res[dbId] + res[dbId1];
                if (threshold !== 'none') {
                    judgeBackgroundColor(result, threshold, item);
                }
            } else {
                result = '--';
            }
            break;
        case 3:
            if (is_numberic(res[dbId]) && is_numberic(res[dbId1]) && is_numberic(res[dbId2])) {
                result = res[dbId] + res[dbId1] - res[dbId2];
                judgeBackgroundColor(result, threshold, item);
            } else {
                result = '--';
            }
            break;
        case 4:
            if (is_numberic(res[dbId]) && is_numberic(res[dbId1])) {
                result = res[dbId] - res[dbId1];
                judgeBackgroundColor(result, threshold, item);
            } else {
                result = '--';
            }
            break;
        case 5:
            if (is_numberic(res[dbId]) && is_numberic(res[dbId1]) && is_numberic(res[dbId2]) && is_numberic(res[dbId3])) {
                result = res[dbId] + res[dbId1] - res[dbId2] - res[dbId3];
                judgeBackgroundColor(result, threshold, item);
            } else {
                result = '--';
            }
            break;
    }
    return result;
};

//处理Dadong PressShop数据函数
var getPressValueDD = function (res, item) {
    var result = 0,
        bdId = item.bdId,
        flag = item.flag,
        threshold = item.threshold,
        valA = res[item['ida']],
        valB = res[item['idb']],
        value = res[bdId];
    switch (flag) {
        case 1:
            if (is_numberic(value)) {
                result = value;
                // console.log("flag2 result:" + result);
            } else {
                result = '--';
            }
            break;
        case 2:
            if (is_numberic(valA) && is_numberic(valB)) {
                result = parseInt((valA - valB) * 10) / 10;
                // console.log("flag2 result:" + result);
                judgeBackgroundColor(result, threshold, item);
            } else {
                result = '--';
            }
            break;
        case 3:
            if (is_numberic(value)) {
                result = value + '%';
            } else {
                result = '--';
            }
            break;
        case 4:
            if (is_numberic(valA) && is_numberic(valB)) {
                result = parseInt((valA - valB) * 10) / 10;
                judgeBackgroundColor(result, threshold, item);
                result = result + '%';
            } else {
                result = '--';
            }
            break;
    }
    return result;
};

//处理Tiexi PressShop数据函数
var getPressValueTX = function (res, item) {
    var result = 0,
        bdId = item.bdId,
        flag = item.flag,
        threshold = item.threshold,
        valA = res[item['ida']],
        valB = res[item['idb']],
        value = res[bdId];
    switch (flag) {
        case 1:
            if (is_numberic(value)) {
                result = value;
            } else {
                result = '--';
            }
            break;
        case 2:
            if (is_numberic(valA) && is_numberic(valB)) {
                result = parseInt((valA - valB) * 10) / 10;
                judgeBackgroundColor(result, threshold, item);
            } else {
                result = '--';
            }
            break;
        case 3:
            if (is_numberic(value)) {
                result = value + '%';
            } else {
                result = '--';
            }
            break;
        case 4:
            if (is_numberic(valA) && is_numberic(valB)) {
                result = parseInt((valA - valB) * 10) / 10;
                judgeBackgroundColor(result, threshold, item);
                result = result + '%';
            } else {
                result = '--';
            }
            break;
    }
    return result;
};

//从API获取Assembly数据函数
var getAssemblyData = function () {
    $.ajax({
        url: '/Dadong/NEX/api/production_volume?type=assembly&' + Date.now(),
        type: 'GET',
        dataType: 'json',
        success: function (res) {
            $.each(libAssembly, function (index, item) {
                item.$elm.html(getAssemblyValue(res, item));
            });
        }
    });
};

//从API获取BodyShop数据函数
var getBodyData = function () {
    $.ajax({
        url: '/Dadong/NEX/api/production_volume?type=body&' + Date.now(),
        type: 'GET',
        dataType: 'json',
        success: function (res) {
            $.each(libBody, function (index, item) {
                item.$elm.html(getBodyValue(res, item));
            });
        }
    });
};

//从API获取PaintShop数据函数
var getPaintData = function () {
    $.ajax({
        url: '/Dadong/NEX/api/production_volume?type=paint&' + Date.now(),
        type: 'GET',
        dataType: 'json',
        success: function (res) {
            $.each(libPaint, function (index, item) {
                item.$elm.html(getPaintValue(res, item));
            });
        }
    });
};

//从API获取Dadong PressShop数据函数
var getPressDataDD = function () {
    $.ajax({
        url: '/api/press?type=dd&' + Date.now(),
        type: 'GET',
        dataType: 'json',
        success: function (res) {
            $.each(libPressDD, function (index, item) {
                // console.log(getPressValueDD(res, item));
                item.$elm.html(getPressValueDD(res, item));
            });
            // console.log(res);
        }
    });
};

//从API获取Tiexi PressShop数据函数
var getPressDataTX = function () {
    $.ajax({
        url: '/api/press?type=tx&' + Date.now(),
        type: 'GET',
        dataType: 'json',
        success: function (res) {
            $.each(libPressTX, function (index, item) {
                item.$elm.html(getPressValueTX(res, item));
            });
            // console.log(res);
        }
    });
};

var getTime = function () {
    var date = new Date();
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    var hour = date.getHours();
    var minute = date.getMinutes();
    var second = date.getSeconds();
    month = checkTime(month);
    day = checkTime(day);
    hour = checkTime(hour);
    minute = checkTime(minute);
    second = checkTime(second);
    var time = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;

    function checkTime(i) {
        if (i < 10) {
            i = "0" + i;
        }
        return i;
    }

    $('.com-time').html(time);
    setTimeout(function () {
        getTime()
    }, 500);
};

var fixPage = function () {
    $.each(urlMark, function (index, item) {
        item.$tabID.click(function () {
            window.location.href = item.mark;
        });
    });
    if (tabId) {
        $.each(urlMark, function (index, item) {
            if (tabId === item.mark) {
                $(".active").removeClass('active');
                item.$tabID.addClass('active');
                item.$tableID.addClass('active in');
            }
        });
    }
    $('.com-tab').on('click', function (e) {
        e.preventDefault();
        $('.active').removeClass('active');
    });
};

$(function () {
    getAssemblyData();
    getPaintData();
    getBodyData();
    getPressDataDD();
    getPressDataTX();
    getTime();
    fixPage();
    th = setInterval(function () {
        getAssemblyData();
        getPaintData();
        getBodyData();
        getPressDataDD();
        getPressDataTX();
    }, 30000);
    setTimeout(setBackground(), 3000);
    $(window).on('resize', function () {
        setBackground();
    });
});